// pages/index/emergencyContact/index.js
import Dialog from '../../../../miniprogram_npm/@vant/weapp/dialog/dialog';
import Toast from '../../../../miniprogram_npm/@vant/weapp/toast/toast';
const $api = require('../../../../request/api').API;
Page({
  /**
   * 页面的初始数据
   */
  data: {
    oneName: '',
    onePhone: '',
    twoName: '',
    twoPhone: '',
    threeName: '',
    threePhone: '',
    helpFlag: 0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {},

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {},

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    $api.getDataByUserId({ userId: wx.getStorageSync('user_id') }).then(res => {
      let data = res.data.data;
      this.setData({
        oneName: data.oneName,
        onePhone: data.onePhone,
        twoName: data.twoName,
        twoPhone: data.twoPhone,
        threeName: data.threeName,
        threePhone: data.threePhone,
        helpFlag: data.helpFlag
      });
    });
  },
  async handleHelpFlagChange(e) {
    await $api.setUserHelpFlag({ helpFlag: e.detail, userId: wx.getStorageSync('user_id') });
    this.setData({
      helpFlag: e.detail
    });
  },
  /* 姓名输入 */
  handleNameInput(e) {
    let type = e.currentTarget.dataset.type;
    let temp = null;
    if (type == 1) temp = 'oneName';
    if (type == 2) temp = 'twoName';
    if (type == 3) temp = 'threeName';
    this.setData({
      [temp]: e.detail.value
    });
  },
  /* 电话号码输入 */
  handlePhoneInput(e) {
    let type = e.currentTarget.dataset.type;
    let temp = null;
    if (type == 1) temp = 'onePhone';
    if (type == 2) temp = 'twoPhone';
    if (type == 3) temp = 'threePhone';
    this.setData({
      [temp]: e.detail.value
    });
  },
  /* 删除输入数据 */
  deleteInput(e) {
    let temp = e.currentTarget.dataset.type;
    this.setData({
      [temp]: ''
    });
  },
  /* 校验电话号码 */
  checkSubmit() {
    let reg = /^\d{1,12}$/;
    if (this.data.onePhone && !reg.test(this.data.onePhone)) {
      Toast.fail('第一紧急联系人手机号格式错误');
      return false;
    }
    if (this.data.twoPhone && !reg.test(this.data.twoPhone)) {
      Toast.fail('第二紧急联系人手机号格式错误');
      return false;
    }
    if (this.data.threePhone && !reg.test(this.data.threePhone)) {
      Toast.fail('第三紧急联系人手机号格式错误');
      return false;
    }
    return true;
  },
  submit() {
    if (this.checkSubmit()) {
      let params = {
        userId: wx.getStorageSync('user_id'),
        oneName: this.data.oneName,
        onePhone: this.data.onePhone,
        twoName: this.data.twoName,
        twoPhone: this.data.twoPhone,
        threeName: this.data.threeName,
        threePhone: this.data.threePhone
      };
      Dialog.confirm({
        title: '提示',
        message: '确认提交吗？'
      }).then(res => {
        console.log(params);
        $api.setUserContact(params).then(res => {
          wx.showToast({
            title: '操作成功'
          });
          setTimeout(() => {
            wx.navigateBack({
              delta: 1
            });
          }, 1000);
        });
      });
    }
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {},

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {},

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {},

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {},

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {}
});
